package com.group02.community.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.entity.user.SysRole;
import com.group02.community.entity.vo.queryVO.RoleQueryVO;
import com.group02.community.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    IRoleService roleService;

    @PostMapping("roles")
    public ResponseEntity<IPage<SysRole>> getRolePage(Long current, Long size, String date, RoleQueryVO rq){
        if (date!=null && date!=""){
            String[] split = date.split(",");
            System.out.println(split.length);
            rq.setStartTime(split[0]);
            rq.setEndTime(split[1]);
        }
        IPage<SysRole> page = roleService.getRolePage(current, size,rq);
        return ResponseEntity.ok(page);
    }


    @GetMapping("allRoles")
    public ResponseEntity<List<SysRole>> getAllRole(){
        List<SysRole> list = roleService.list(null);
        return ResponseEntity.ok(list);
    }

    @GetMapping("role")
    public ResponseEntity<SysRole> getRoleById(Long id){
        SysRole role=roleService.getRoleById(id);
        return ResponseEntity.ok(role);
    }

    @PostMapping("role")
    public ResponseEntity<String> addRole(SysRole role){
        String msg=roleService.addAllRoleMsg(role);
        return ResponseEntity.ok(msg);
    }

    @PutMapping("role")
    public ResponseEntity<String> updateRole(@RequestBody SysRole role){
        String msg=roleService.updateAllRoleMsg(role);
        return ResponseEntity.ok(msg);
    }



}
